﻿var equipmentDetailFormVue = new Vue({
    el: "#equipmentDetailForm",
    data: {
        isNewEquipment: true,   //判断是否新设备
        isEditMode: false,      //判断可否编辑
        equipment: {
            BaseInfo: {},
            MeasureInfo: {},
            MaintainInfo: {},
            MsaInfo: {},
            AssignerInfo: {},
            ManagerInfo: {}
        },
        statusDictionary: {},       //设备状态字典
        typeDictionary: {},         //设备类型字典
        isNeedDictionary: {},       //是与否字典
        warnDaysDictionary: {},     //预警日数字典
        periodDaysDictionary: {},   //周期日数字典
        measureTypeDictionary: {},  //计量类型字典
        maintainTypeDictionary: {}, //保养类型字典
        msaTypeDictionary: {}       //Msa类型字典
    },
    methods: {
        SaveEquipment: function (event) {
            event.preventDefault();
            SaveEquipmentDetail(this);
        },
        EditEquipment: function (event) {
            event.preventDefault();
            this.isEditMode = true;
            ResetFromEditMode(this);
        },
        UpdateEquipment: function (event) {
            event.preventDefault();
            UpdateEquipmentDetail(this);
        },
        CancelEditEquipment: function (event) {
            event.preventDefault();
            this.isEditMode = false;
            ResetFromEditMode(this);
        },
        GetAssingerInfo: function () {
            GetAssignerInfoByName(this, this.equipment.AssignerInfo.NameChs);
        },
        GetManagerInfo: function () {
            GetManagerInfoByName(this, this.equipment.ManagerInfo.NameChs);
        }

    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.statusDictionary = GlobalDictionary.GetEquipmentStatusDictionary();
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.isNeedDictionary = GlobalDictionary.GetTrueOrFalseDictionary();
        this.warnDaysDictionary = GlobalDictionary.GetEquipmentWarnDaysDictionary();
        this.periodDaysDictionary = GlobalDictionary.GetEquipmentPeriodDaysDictionary();
        this.measureTypeDictionary = GlobalDictionary.GetMeasureTypeDictionary();
        this.maintainTypeDictionary = GlobalDictionary.GetMaintainTypeDictionary();
        this.msaTypeDictionary = GlobalDictionary.GetMsaTypeDictionary();

        var isNew = GetQueryString("IsNew");
        var equipmentID = GetQueryString("EquipmentID");
        if (isNew == "true") {
            this.navTitle = "新增设备信息New equipment information";
            this.isNewEquipment = true;
            this.isEditMode = true;
        } else {
            this.navTitle = "编辑设备信息Edit device information";
            this.isNewEquipment = false;
            this.isEditMode = false;
            GetEquipmentDetail(this, equipmentID);
        }
        ResetFromEditMode(this);

        //设置日期时间选择器
        $(".azDatePicker").datetimepicker({
            format: "yyyy-mm-dd",
            autoclose: true,
            minView: 2
        });
        var vue = this;
        //由于datetimepicker插件不能让Vue双向绑定数据生效，所以需监听事件赋值
        $("#equipmentPurchaseDate").datetimepicker().on("hide", function (event) {
            vue.equipment.BaseInfo.PurchaseDate = $(event.target).val();
        });
        $("#equipmentWarrantyDate").datetimepicker().on("hide", function (event) {
            vue.equipment.BaseInfo.WarrantyDate = $(event.target).val();
        });
        $("#lastMeasureDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MeasureInfo.LastMeasureDate = $(event.target).val();
        });
        $("#warnMeasureDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MeasureInfo.WarnMeasureDate = $(event.target).val();
        });
        $("#nextMeasureDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MeasureInfo.NextMeasureDate = $(event.target).val();
        });
        $("#lastMaintainDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MaintainInfo.LastMaintainDate = $(event.target).val();
        });
        $("#warnMaintainDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MaintainInfo.WarnMaintainDate = $(event.target).val();
        });
        $("#nextMaintainDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MaintainInfo.NextMaintainDate = $(event.target).val();
        });
        $("#lastMsaDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MsaInfo.LastMsaDate = $(event.target).val();
        });
        $("#warnMsaDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MsaInfo.WarnMsaDate = $(event.target).val();
        });
        $("#nextMsaDate").datetimepicker().on("hide", function (event) {
            vue.equipment.MsaInfo.NextMsaDate = $(event.target).val();
        });
    },
    watch: {
        equipment: {
            handler: function (val, oldVal) {
                if (this.isEditMode) {
                    if (val.BaseInfo.Code)
                        CheckEquipmentCode(val.BaseInfo.Code);
                    if (val.BaseInfo.Name)
                        CheckEquipmentName(val.BaseInfo.Code);
                    if (val.BaseInfo.Model)
                        CheckEquipmentModel(val.BaseInfo.Model);
                    if (val.BaseInfo.Sn)
                        CheckEquipmentSn(val.BaseInfo.Sn);
                    if (val.BaseInfo.Status)
                        CheckEquipmentStatus(val.BaseInfo.Status);
                    if (val.BaseInfo.AssignerID)
                        CheckEquipmentAssingerInfo(val.BaseInfo.AssignerID);
                    if (val.BaseInfo.ManagerID)
                        CheckEquipmentManagerInfo(val.BaseInfo.ManagerID);
                    if (val.MeasureInfo.IsNeed)
                        CheckEquipmentMeasureIsNeed(val.MeasureInfo.IsNeed);
                    if (val.MeasureInfo.Type)
                        CheckEquipmentMeasureType(val.MeasureInfo.Type);
                    if (val.MeasureInfo.Period)
                        CheckEquipmentMeasurePeriod(val.MeasureInfo.Period);
                    if (val.MaintainInfo.Warning)
                        CheckEquipmentMeasureWarning(val.MeasureInfo.Warning);
                    if (val.MaintainInfo.IsNeed)
                        CheckEquipmentMaintainIsNeed(val.MaintainInfo.IsNeed);
                    if (val.MaintainInfo.Type)
                        CheckEquipmentMaintainType(val.MaintainInfo.Type);
                    if (val.MaintainInfo.Period)
                        CheckEquipmentMaintainPeriod(val.MaintainInfo.Period);
                    if (val.MaintainInfo.Warning)
                        CheckEquipmentMaintainWarning(val.MaintainInfo.Warning);
                    if (val.MsaInfo.IsNeed)
                        CheckEquipmentMsaIsNeed(val.MsaInfo.IsNeed);
                    if (val.MsaInfo.Type)
                        CheckEquipmentMsaType(val.MsaInfo.Type);
                    if (val.MsaInfo.Period)
                        CheckEquipmentMsaPeriod(val.MsaInfo.Period);
                    if (val.MsaInfo.Warning)
                        CheckEquipmentMsaWarning(val.MsaInfo.Warning);
                }
            },
            deep: true
        }
    }
});

/**
 * 获取设备详细信息
 */
function GetEquipmentDetail(vue, equipmentID) {
    parent.ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Equipment/GetEquipmentByID?EquipmentID=" + equipmentID + "&v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                console.log(JSON.stringify(res.data));
                SetEquipmentDetail(vue, res.data);
            } else {
                toastr.error(res.message);
            }
            parent.HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            parent.HideAzLoading();
            toastr.error("获取设备信息失败，请稍候再试Failed to get device information. Please try again later。");
        }
    });
}


/**
 * 根据用户名查询负责人信息
 */
function GetAssignerInfoByName(vue, userName) {
    if (userName != null && userName.length > 0) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "GET",
            url: "/Account/GetUserByName?userName=" + userName + "&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    //设置负责人信息
                    vue.equipment.AssignerInfo = res.data;
                    vue.equipment.BaseInfo.AssignerID = vue.equipment.AssignerInfo.ID;
                    $("#equipmentAssignerInfo").parent().removeClass("has-error");
                } else {
                    toastr.error(res.message);
                    vue.equipment.AssignerInfo = {};
                    vue.equipment.BaseInfo.AssignerID = 0;
                    $("#equipmentAssignerInfo").parent().addClass("has-error");
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    } else {
        vue.equipment.AssignerInfo = {};
    }
}

/**
 * 根据用户名查询管理人信息
 */
function GetManagerInfoByName(vue, userName) {
    if (userName != null && userName.length > 0) {
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "GET",
            url: "/Account/GetUserByName?userName=" + userName + "&v=" + Math.random(),
            success: function (res) {
                if (res.status == "Success") {
                    //设置负责人信息
                    vue.equipment.ManagerInfo = res.data;
                    vue.equipment.BaseInfo.ManagerID = vue.equipment.ManagerInfo.ID;
                    $("#equipmentManagerInfo").parent().removeClass("has-error");
                } else {
                    toastr.error(res.message);
                    vue.equipment.ManagerInfo = {};
                    vue.equipment.BaseInfo.ManagerID = 0;
                    $("#equipmentManagerInfo").parent().addClass("has-error");
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    } else {
        vue.equipment.ManagerInfo = {};
    }
}

/**
 * 解析服务器返回的设备所有详细信息
 */
function SetEquipmentDetail(vue, info) {
    vue.equipment = info;

    vue.equipment.BaseInfo.PurchaseDate = CSharpToJsDate(vue.equipment.BaseInfo.PurchaseDate);
    vue.equipment.BaseInfo.WarrantyDate = CSharpToJsDate(vue.equipment.BaseInfo.WarrantyDate);
    vue.equipment.MeasureInfo.WarnMeasureDate = CSharpToJsDate(vue.equipment.MeasureInfo.WarnMeasureDate);
    vue.equipment.MeasureInfo.LastMeasureDate = CSharpToJsDate(vue.equipment.MeasureInfo.LastMeasureDate);
    vue.equipment.MeasureInfo.NextMeasureDate = CSharpToJsDate(vue.equipment.MeasureInfo.NextMeasureDate);
    vue.equipment.MaintainInfo.WarnMaintainDate = CSharpToJsDate(vue.equipment.MaintainInfo.WarnMaintainDate);
    vue.equipment.MaintainInfo.LastMaintainDate = CSharpToJsDate(vue.equipment.MaintainInfo.LastMaintainDate);
    vue.equipment.MaintainInfo.NextMaintainDate = CSharpToJsDate(vue.equipment.MaintainInfo.NextMaintainDate);
    vue.equipment.MsaInfo.WarnMsaDate = CSharpToJsDate(vue.equipment.MsaInfo.WarnMsaDate);
    vue.equipment.MsaInfo.LastMsaDate = CSharpToJsDate(vue.equipment.MsaInfo.LastMsaDate);
    vue.equipment.MsaInfo.NextMsaDate = CSharpToJsDate(vue.equipment.MsaInfo.NextMsaDate);
    
}

/**
 * 保存设备信息
 */
function SaveEquipmentDetail(vue) {
    if (CheckEquipmentDetail(vue)) {
        vue.equipment.BaseInfo.TypeID = vue.typeDictionary[vue.equipment.BaseInfo.Name];
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Equipment/SaveEquipment",
            data: { equipment: vue.equipment },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存设备信息成功Device information saved successfully。");
                    ResetForm(vue);
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 更新设备信息
 */
function UpdateEquipmentDetail(vue) {
    if (CheckEquipmentDetail(vue)) {
        console.log(vue.equipment);
        vue.equipment.BaseInfo.TypeID = vue.typeDictionary[vue.equipment.BaseInfo.Name];
        parent.ShowAzLoading();
        $("button").attr("disabled", true);
        $.ajax({
            type: "POST",
            url: "/Equipment/UpdateEquipment",
            data: { equipment: vue.equipment },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存设备信息成功Device information saved successfully。");
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            },
            complete: function () {
                parent.HideAzLoading();
                $("button").removeAttr("disabled");
            }
        });
    }
}

/**
 * 设置表格中所有输入框可否编辑
 */
function ResetFromEditMode(vue) {
    if (vue.isEditMode) {
        $("input").removeAttr("disabled");
        $("select").removeAttr("disabled");
    } else {
        $("input").attr("disabled", "disabled");
        $("select").attr("disabled", "disabled");
    }       
}

/**
 * 清空表单信息
 */
function ResetForm(vue) {
    vue.equipment = {
        BaseInfo: {},
        MeasureInfo: {},
        MaintainInfo: {},
        MsaInfo: {},
        AssignerInfo: {},
        ManagerInfo: {}
    };
}

/**
 * 检查设备信息正确性
 */
function CheckEquipmentDetail(vue) {
    if (!CheckEquipmentCode(vue.equipment.BaseInfo.Code)) {
        toastr.error("请填写正确设备编号Please fill in the correct equipment number。<br>[格式format：XX-001]");
        return false;
    } else if (!CheckEquipmentName(vue.equipment.BaseInfo.Name)) {
        toastr.error("请选择设备名称Please select a device name。");
        return false;
    } else if (!CheckEquipmentModel(vue.equipment.BaseInfo.Model)) {
        toastr.error("请填写设备型号Please fill in the equipment model。");
        return false;
    } else if (!CheckEquipmentSn(vue.equipment.BaseInfo.Sn)) {
        toastr.error("请填写设备序列号Please fill in the equipment serial number。");
        return false;
    } else if (!CheckEquipmentStatus(vue.equipment.BaseInfo.Status)) {
        toastr.error("请选择设备状态Please select device status。");
        return false;
    } else if (!CheckEquipmentAssingerInfo(vue.equipment.BaseInfo.AssignerID)) {
        toastr.error("请填写负责人名字(中文或英文)Please fill in the name of the person in charge (Chinese or English)。");
        return false;
    } else if (!CheckEquipmentManagerInfo(vue.equipment.BaseInfo.ManagerID)) {
        toastr.error("请填写管理人名字(中文或英文)Please fill in the name of the manager (Chinese or English)。");
        return false;
    } else if (vue.equipment.BaseInfo.ManagerID == vue.equipment.BaseInfo.AssignerID) {
        toastr.error("请检查，负责人与管理人不能为同一个人Please check that the person in charge and the manager cannot be the same person。");
        return false;
    }
    //else if (!CheckEquipmentMeasureIsNeed(vue.equipment.MeasureInfo.IsNeed)) {
    //    toastr.error("请选择设备是否需要计量？");
    //    return false;
    //} else if (!CheckEquipmentMeasureType(vue.equipment.MeasureInfo.Type)) {
    //    toastr.error("请选择设备计量类型。");
    //    return false;
    //} else if (!CheckEquipmentMeasurePeriod(vue.equipment.MeasureInfo.Period)) {
    //    toastr.error("请选择设备计量周期日数。");
    //    return false;
    //} else if (!CheckEquipmentMeasureWarning(vue.equipment.MeasureInfo.Warning)) {
    //    toastr.error("请选择设备计量预警日数。");
    //    return false;
    //} else if (!CheckEquipmentMaintainIsNeed(vue.equipment.MaintainInfo.IsNeed)) {
    //    toastr.error("请选择设备是否需要保养？");
    //    return false;
    //} else if (!CheckEquipmentMaintainType(vue.equipment.MaintainInfo.Type)) {
    //    toastr.error("请选择设备保养类型。");
    //    return false;
    //} else if (!CheckEquipmentMaintainPeriod(vue.equipment.MaintainInfo.Period)) {
    //    toastr.error("请选择设备保养周期日数。");
    //    return false;
    //} else if (!CheckEquipmentMaintainWarning(vue.equipment.MaintainInfo.Warning)) {
    //    toastr.error("请选择设备保养预警日数。");
    //    return false;
    //} else if (!CheckEquipmentMsaIsNeed(vue.equipment.MsaInfo.IsNeed)) {
    //    toastr.error("请选择设备是否需要测量系统分析？");
    //    return false;
    //} else if (!CheckEquipmentMsaType(vue.equipment.MsaInfo.Type)) {
    //    toastr.error("请选择设备测量系统分析类型。");
    //    return false;
    //} else if (!CheckEquipmentMsaPeriod(vue.equipment.MsaInfo.Period)) {
    //    toastr.error("请选择设备测量系统分析周期日数。");
    //    return false;
    //} else if (!CheckEquipmentMsaWarning(vue.equipment.MsaInfo.Warning)) {
    //    toastr.error("请选择设备测量系统分析预警日数。");
    //    return false;
    //} 
    return true;
}

function CheckEquipmentCode(value) {
    var reg = /^[A-Za-z0-9-]{2,20}$/;
    var result = reg.test(value);
    
    $("#equipmentCode").parent().removeClass("has-error");
    if (!value || !result) {
        result = false;
        $("#equipmentCode").parent().addClass("has-error");
    }
    return result;
}

function CheckEquipmentName(value) {
    $("#equipmentName").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#equipmentName").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentModel(value) {
    $("#equipmentModel").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#equipmentModel").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentSn(value) {
    $("#equipmentSn").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#equipmentSn").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentStatus(value) {
    $("#equipmentStatus").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#equipmentStatus").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentAssingerInfo(assingerID) {
    $("#equipmentAssignerInfo").parent().removeClass("has-error");
    if (!assingerID || assingerID.length == 0 || assingerID == 0) {
        $("#equipmentAssignerInfo").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentManagerInfo(managerID) {
    $("#equipmentManagerInfo").parent().removeClass("has-error");
    if (!managerID || managerID.length == 0 || managerID == 0) {
        $("#equipmentManagerInfo").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMeasureIsNeed(value) {
    $("#measureIsNeed").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#measureIsNeed").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMeasureType(value) {
    $("#measureType").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#measureType").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMeasureWarning(value) {
    $("#measureWarning").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#measureWarning").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMeasurePeriod(value) {
    $("#measurePeriod").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#measurePeriod").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMaintainIsNeed(value) {
    $("#maintainIsNeed").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#maintainIsNeed").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMaintainType(value) {
    $("#maintainType").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#maintainType").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMaintainWarning(value) {
    $("#maintainWarning").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#maintainWarning").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMaintainPeriod(value) {
    $("#maintainPeriod").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#maintainPeriod").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMsaIsNeed(value) {
    $("#msaIsNeed").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#msaIsNeed").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMsaType(value) {
    $("#msaType").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#msaType").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMsaWarning(value) {
    $("#msaWarning").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#msaWarning").parent().addClass("has-error");
        return false;
    }
    return true;
}

function CheckEquipmentMsaPeriod(value) {
    $("#msaPeriod").parent().removeClass("has-error");
    if (!value || value.length == 0) {
        $("#msaPeriod").parent().addClass("has-error");
        return false;
    }
    return true;
}
